#!/bin/bash

hostname mysql.example.com

# Install Mysql Client / Server
if [ ! -d /etc/init.d/mysqld ]; then
	sudo yum -y install mysql mysql-server
	chckconfig mysqld on
	/etc/init.d/mysqld start
fi

# Import SafariOnline database

yum -y install unzip
if [ ! -d /usr/src/relatedfiles ]; then
	if [ -f /vagrant/sql.zip ]; then	
		unzip /vagrant/sql.zip -d /usr/src	
		if [ -d /usr/src/relatedfiles ]; then
			cd /usr/src/relatedfiles
			if [ -f LiveLessonSQL_Lesson1.sql ]; then
				echo "Importing LiveLessonSQL_Lesson1.sql "
				sudo mysql < LiveLessonSQL_Lesson1.sql
			fi
		fi
		
	fi
fi

# Import sakila database

if [ ! -d /usr/src/sakila-db ]; then
	cd /vagrant
	wget http://downloads.mysql.com/docs/sakila-db.tar.gz
	if [ -f /vagrant/sakila-db.tar.gz ]; then 
		sudo tar xvf /vagrant/sakila-db.tar.gz -C /usr/src
		if [ -d /usr/src/sakila-db ]; then
			cd /usr/src/sakila-db
			for file in sakila-schema.sql sakila-data.sql; do
				if [ -f $file ]; then
					echo "Importing $file"
					sudo mysql < $file
				fi
			done
		fi
	else
		echo "Unable to download sakila-db.tar.gz"
	fi
	rm -f /vagrant/sakila-db.tar.gz*
fi

